<?php
/**
 * ClassName: user
 * Description: 用户控制器
 * @Author 周昌
 * @Create 2023/11/15 14:04
 */

namespace app;


use epii\server\Args;
use model\MemberModel;
use service\admin\HospitalService;
use service\admin\MemberService;

class member extends base
{
    protected $admin_allow=['index','ajax_data','add_edit','status','del'];
    public function init()
    {
        parent::init();
        $this->accessDenied();
    }
    public function index()
    {
        if($this->adminInfo['role']==1){
            $this->assign('hospitals',HospitalService::getSelect());
        }
        $this->adminUiDisplay();
    }

    public function ajax_data()
    {
        $map = [];
        //姓名
        $real_name = Args::params('real_name');
        if ($real_name) {
            $map[] = ['real_name', "LIKE", '%' . $real_name . '%'];
        }
        //手机号
        $phone=Args::params('phone');
        if($phone){
            $map[]=['phone','=',$phone];
        }
        $id_card=Args::params('id_card');
        if($id_card){
            $map[]=['id_card','=',$id_card];
        }
        echo MemberService::getAjaxData($map);
    }

    public function add_edit()
    {
        $id = Args::params('id');
        if ($_SERVER['REQUEST_METHOD'] == "POST") {
            $data = Args::params('r');
            //登录权限判断
            $hospital_id=$this->adminInfo['hospital_id'];
            if(!empty($hospital_id)){
                $data['hospital_id']=$hospital_id;
            }
            //验证
            $this->validate($data,[
                'phone|手机号'=>'require|mobile',
                'real_name|姓名'=>'require|chsAlphaNum',
                'sex|性别'=>'require|integer',
                'hospital_id|所属医院'=>'require|integer|>=:0',//当前选择或默认的医院id
                'status|状态'=>'require|integer',
            ]);
            //判断用户是否已经存在
            $res=MemberService::memberIsExist($id,$data['phone']);
            if(!$res['code']){
                $this->error($res['msg']);
            }
            if ($id) {
                $res = MemberService::edit($id,$data);
            } else {
                $data['addtime']=time();
//                $res = MemberService::add($data);
                $res=MemberService::addMemberAndInfo($data);
            }
            $res['code']?$this->success($res['msg']):$this->error($res['msg']);
        }
        $this->_as_info = $id ? MemberModel::getInfo(['id'=>$id]) : [];
        $this->_as_id = $id;
        $this->_as_status = MemberService::statusSelect();
        $this->_as_sex = MemberService::memberSexSelect();
        $this->_as_hospitals = HospitalService::getSelect();

        $this->adminUiDisplay();
    }
    //修改状态
    public function status(){
        $id=Args::params('id/d/1','id,不能为空');
        $status=Args::params('status');
        $res=MemberService::status($id,$status);
        if($res['code']){
            $this->success('成功','refresh');
        }
        $this->error();
    }

    /**
     * @return array|false|string* 删除
     */
    public function del()
    {
        $id = Args::params('id');
        //todo 判断用户下是否已有会员卡，无则可以删除
        $res = MemberService::del($id);
        if ($res['code']) {
            $this->success($res['msg'],'refresh');
        } else {
            $this->error($res['msg']);
        }
    }

}